package cn.edu.cuc.yjsy.admin.util;

import java.text.MessageFormat;

import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.Status;
import org.osgi.framework.Bundle;

public class LoggerAdvisor implements ILogger {

	private String pluginName;				
	
	public LoggerAdvisor(Class clazz){
		pluginName = clazz.getPackage().getName();
	}
	/* (non-Javadoc)
	 * @see cn.edu.cuc.yjsy.admin.util.ILogger#isErrorEnabled()
	 */
	public boolean isErrorEnabled() {
		return true;
	}

	/* (non-Javadoc)
	 * @see cn.edu.cuc.yjsy.admin.util.ILogger#isFatalEnabled()
	 */
	public boolean isFatalEnabled() {
		return true;
	}

	/* (non-Javadoc)
	 * @see cn.edu.cuc.yjsy.admin.util.ILogger#isInfoEnabled()
	 */
	public boolean isInfoEnabled() {
		return true;
	}

	/* (non-Javadoc)
	 * @see cn.edu.cuc.yjsy.admin.util.ILogger#isTraceDebugEnabled()
	 */
	public boolean isTraceDebugEnabled() {
		return true;
	}

	/* (non-Javadoc)
	 * @see cn.edu.cuc.yjsy.admin.util.ILogger#isTraceEnabled()
	 */
	public boolean isTraceEnabled() {
		return true;
	}

	/* (non-Javadoc)
	 * @see cn.edu.cuc.yjsy.admin.util.ILogger#isTraceEntryExitEnabled()
	 */
	public boolean isTraceEntryExitEnabled() {
		return true;
	}

	/* (non-Javadoc)
	 * @see cn.edu.cuc.yjsy.admin.util.ILogger#isTraceEventEnabled()
	 */
	public boolean isTraceEventEnabled() {
		return true;
	}

	/* (non-Javadoc)
	 * @see cn.edu.cuc.yjsy.admin.util.ILogger#isWarnEnabled()
	 */
	public boolean isWarnEnabled() {
		return true;
	}

	/* (non-Javadoc)
	 * @see cn.edu.cuc.yjsy.admin.util.ILogger#trace(java.lang.String, java.lang.Throwable)
	 */
	public void trace(String arg0, Throwable arg1) {
		logTrace(arg0, null, null, arg1);
	}

	/* (non-Javadoc)
	 * @see cn.edu.cuc.yjsy.admin.util.ILogger#trace(java.lang.String)
	 */
	public void trace(String arg0) {
		logTrace(arg0, null, null, null);
	}

	/* (non-Javadoc)
	 * @see cn.edu.cuc.yjsy.admin.util.ILogger#traceEntry(java.lang.Object, java.lang.String)
	 */
	public void traceEntry(Object arg0, String arg1) {		
		logTrace(getSimpleClassName(arg0), arg1, "Entry", null);
	}
	

	/* (non-Javadoc)
	 * @see cn.edu.cuc.yjsy.admin.util.ILogger#traceExit(java.lang.Object, java.lang.String)
	 */
	public void traceExit(Object arg0, String arg1) {		
		logTrace(getSimpleClassName(arg0), arg1, "Exit", null);
	}


	/* (non-Javadoc)
	 * @see cn.edu.cuc.yjsy.admin.util.ILogger#traceEvent(java.lang.Object, java.lang.String, java.lang.String)
	 */
	public void traceEvent(Object arg0, String arg1, String arg2) {
		logTrace(getSimpleClassName(arg0), arg1, arg2, null);
	}


	/* (non-Javadoc)
	 * @see cn.edu.cuc.yjsy.admin.util.ILogger#traceEvent(java.lang.Object, java.lang.String, java.lang.String, java.lang.Throwable)
	 */
	public void traceEvent(Object arg0, String arg1, String arg2, Throwable arg3) {		
		logTrace(getSimpleClassName(arg0), arg1, arg2, arg3);
	}

	/* (non-Javadoc)
	 * @see cn.edu.cuc.yjsy.admin.util.ILogger#traceEvent(java.lang.String, java.lang.String, java.lang.String, java.lang.Throwable)
	 */
	public void traceEvent(String arg0, String arg1, String arg2, Throwable arg3){	
		logTrace(arg0, arg1, arg2, arg3);
	}

	/* (non-Javadoc)
	 * @see cn.edu.cuc.yjsy.admin.util.ILogger#error(java.lang.String, java.lang.Object[], java.lang.Throwable)
	 */
	public void error(String arg0, Object[] arg1, Throwable arg2) {
		logError(arg0, null, arg1, arg2);
	}

	/* (non-Javadoc)
	 * @see cn.edu.cuc.yjsy.admin.util.ILogger#error(java.lang.String, java.lang.Object[])
	 */
	public void error(String arg0, Object[] arg1) {
		logError(arg0, null, arg1, null);
	}

	/* (non-Javadoc)
	 * @see cn.edu.cuc.yjsy.admin.util.ILogger#error(java.lang.String, java.lang.String, java.lang.Object[], java.lang.Throwable)
	 */
	public void error(String arg0, String arg1, Object[] arg2, Throwable arg3) {		
		logError(arg0, arg1, arg2, arg3);
	}

	/* (non-Javadoc)
	 * @see cn.edu.cuc.yjsy.admin.util.ILogger#error(java.lang.String, java.lang.String, java.lang.Object[])
	 */
	public void error(String arg0, String arg1, Object[] arg2) {
		logError(arg0, arg1, arg2, null);		
	}

	/* (non-Javadoc)
	 * @see cn.edu.cuc.yjsy.admin.util.ILogger#error(java.lang.String, java.lang.String)
	 */
	public void error(String arg0, String arg1) {
		logError(arg0, arg1, null, null);
	}

	/* (non-Javadoc)
	 * @see cn.edu.cuc.yjsy.admin.util.ILogger#error(java.lang.String)
	 */
	public void error(String arg0) {
		logError(arg0, null, null, null);		
	}

	/* (non-Javadoc)
	 * @see cn.edu.cuc.yjsy.admin.util.ILogger#fatal(java.lang.String, java.lang.Object[], java.lang.Throwable)
	 */
	public void fatal(String arg0, Object[] arg1, Throwable arg2) {
		logError(arg0, null, arg1, arg2);
	}

	/* (non-Javadoc)
	 * @see cn.edu.cuc.yjsy.admin.util.ILogger#fatal(java.lang.String, java.lang.Object[])
	 */
	public void fatal(String arg0, Object[] arg1) {
		logError(arg0, null, arg1, null);		
	}

	/* (non-Javadoc)
	 * @see cn.edu.cuc.yjsy.admin.util.ILogger#fatal(java.lang.String, java.lang.String, java.lang.Object[], java.lang.Throwable)
	 */
	public void fatal(String arg0, String arg1, Object[] arg2, Throwable arg3) {
		logError(arg0, arg1, arg2, arg3);	
	}

	/* (non-Javadoc)
	 * @see cn.edu.cuc.yjsy.admin.util.ILogger#fatal(java.lang.String, java.lang.String, java.lang.Object[])
	 */
	public void fatal(String arg0, String arg1, Object[] arg2) {
		logError(arg0, arg1, arg2, null);
	}

	/* (non-Javadoc)
	 * @see cn.edu.cuc.yjsy.admin.util.ILogger#fatal(java.lang.String, java.lang.String)
	 */
	public void fatal(String arg0, String arg1) {
		logError(arg0, arg1, null, null);		
	}

	/* (non-Javadoc)
	 * @see cn.edu.cuc.yjsy.admin.util.ILogger#fatal(java.lang.String)
	 */
	public void fatal(String arg0) {
		logError(arg0, null, null, null);
	}

	/* (non-Javadoc)
	 * @see cn.edu.cuc.yjsy.admin.util.ILogger#info(java.lang.String, java.lang.Object[], java.lang.Throwable)
	 */
	public void info(String arg0, Object[] arg1, Throwable arg2) {
		logInfo(arg0, null, arg1, arg2);
	}

	/* (non-Javadoc)
	 * @see cn.edu.cuc.yjsy.admin.util.ILogger#info(java.lang.String, java.lang.Object[])
	 */
	public void info(String arg0, Object[] arg1) {
		logInfo(arg0, null, arg1, null);
	}

	/* (non-Javadoc)
	 * @see cn.edu.cuc.yjsy.admin.util.ILogger#info(java.lang.String, java.lang.String, java.lang.Object[], java.lang.Throwable)
	 */
	public void info(String arg0, String arg1, Object[] arg2, Throwable arg3) {
		logInfo(arg0, arg1, arg2, arg3);
	}

	/* (non-Javadoc)
	 * @see cn.edu.cuc.yjsy.admin.util.ILogger#info(java.lang.String, java.lang.String, java.lang.Object[])
	 */
	public void info(String arg0, String arg1, Object[] arg2) {
		logInfo(arg0, arg1, arg2, null);
	}

	/* (non-Javadoc)
	 * @see cn.edu.cuc.yjsy.admin.util.ILogger#info(java.lang.String, java.lang.String)
	 */
	public void info(String arg0, String arg1) {
		logInfo(arg0, arg1, null, null);		
	}

	/* (non-Javadoc)
	 * @see cn.edu.cuc.yjsy.admin.util.ILogger#info(java.lang.String)
	 */
	public void info(String arg0) {
		logInfo(arg0, null, null, null);
	}

	/* (non-Javadoc)
	 * @see cn.edu.cuc.yjsy.admin.util.ILogger#warn(java.lang.String, java.lang.Object[], java.lang.Throwable)
	 */
	public void warn(String arg0, Object[] arg1, Throwable arg2) {
		logWarning(arg0, null, arg1, arg2);
	}

	/* (non-Javadoc)
	 * @see cn.edu.cuc.yjsy.admin.util.ILogger#warn(java.lang.String, java.lang.Object[])
	 */
	public void warn(String arg0, Object[] arg1) {
		logWarning(arg0, null, arg1, null);		
	}

	/* (non-Javadoc)
	 * @see cn.edu.cuc.yjsy.admin.util.ILogger#warn(java.lang.String, java.lang.String, java.lang.Object[], java.lang.Throwable)
	 */
	public void warn(String arg0, String arg1, Object[] arg2, Throwable arg3) {
		logWarning(arg0, arg1, arg2, arg3);
	}

	/* (non-Javadoc)
	 * @see cn.edu.cuc.yjsy.admin.util.ILogger#warn(java.lang.String, java.lang.String, java.lang.Object[])
	 */
	public void warn(String arg0, String arg1, Object[] arg2) {
		logWarning(arg0, arg1, arg2, null);		
	}

	/* (non-Javadoc)
	 * @see cn.edu.cuc.yjsy.admin.util.ILogger#warn(java.lang.String, java.lang.String)
	 */
	public void warn(String arg0, String arg1) {
		logWarning(arg0, arg1, null, null);
	}

	/* (non-Javadoc)
	 * @see cn.edu.cuc.yjsy.admin.util.ILogger#warn(java.lang.String)
	 */
	public void warn(String arg0) {
		logWarning(arg0, null, null, null);		
	}
	
	private String getSimpleClassName(Object obj){
		String name = obj.getClass().getName();
		int pos = name.lastIndexOf(".");
		return name.substring(pos+1); 
	}
	

	/*
	 * We map the trace in rcp logger to info of OSGI logger
	 * Attention: we will not translate the trace message in this message.
	 */
	private void logTrace(String classname, String methodname, String message, Throwable exception){
		StringBuffer msgbuffer = new StringBuffer(classname);
		if(methodname != null){
			msgbuffer.append(" ");		
			msgbuffer.append(methodname);
		}
		if(message != null){
			msgbuffer.append(" ");
			msgbuffer.append(message);
		}
		IStatus status = new Status(IStatus.INFO, pluginName, IStatus.OK, msgbuffer.toString(), exception);
		Bundle bundle = Platform.getBundle(pluginName);
		Platform.getLog(bundle).log(status);
	}
	
	/*
	private void logOk(String message, String situmessage, Object args[], Throwable exception){
		logMessage(IStatus.OK, message, args, exception);
	}*/

	private void logInfo(String message, String situmessage, Object args[], Throwable exception){
		logMessage(IStatus.INFO, message + " " + situmessage, args, exception);
	}
	
	private void logWarning(String message, String situmessage, Object args[], Throwable exception){
		logMessage(IStatus.WARNING, message + " " + situmessage, args, exception);
	}
	
	private void logError(String message, String situmessage, Object args[], Throwable exception){
		logMessage(IStatus.ERROR, message + " " + situmessage, args, exception);
	}	
	
	/*
	private void logCancel(String message, String situmessage, Object args[], Throwable exception){
		logMessage(IStatus.CANCEL, message, args, exception);
	}*/	

	
	private void logMessage(int severity, String message, Object args[], Throwable exception){
		String msgText = MessageFormat.format(message, args);	
		IStatus status = new Status(severity, pluginName, IStatus.OK, msgText, exception);
		Bundle bundle = Platform.getBundle(pluginName);
		Platform.getLog(bundle).log(status);
	}
		
}
